package com.fametal.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.fametal.R;
import com.fametal.GlobalConstant.Settings;
import com.fametal.ui.service.QTService;
import com.fametal.ui.utils.ThreadPool;
import com.fametal.ui.utils.Utils;

public class ExitActivity extends Activity implements OnClickListener {
	
	private TextView tip;
	private TextView exittip;
	private TextView exiting;
	private LinearLayout layout;
	private Button exit_system;
	private Button back_system;
	private ProgressBar exit_process;
	
	private final int tblRelayDelay[] = {
			5,
			15,
			25,
			35,
			45,
			95,
		};
		
	private final String tblRelayNo[] = {
			"0",
			"1",
			"4",
			"3",
			"5",
			"2"
	};

	
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			exit_process.setProgress(msg.arg1);
			
			for(int i=5;i>0;i--){
				if(msg.arg1 == tblRelayDelay[i]){
					String url = Settings.IP + "cgi-bin/relay_control.cgi?relayNo=" + tblRelayNo[i]+"&relayStatus=0";
					Utils.get(getParent(), url, false);
					break;
				}
			}
			
			if(msg.arg1 == 100){
				String url = Settings.IP + "cgi-bin/login.cgi?operation=exit";
				Utils.get(getParent(), url, false);
				Intent intent = new Intent();
				intent.setClass(getApplicationContext(), LoginActivity.class);
				startActivity(intent);
				finish();
			}
		};
	};
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.exit);
		
		tip = (TextView) findViewById(R.id.tip);
		exittip = (TextView) findViewById(R.id.exittip);
		layout = (LinearLayout) findViewById(R.id.layout);
		exit_system = (Button) findViewById(R.id.exit_system);
		back_system = (Button) findViewById(R.id.back_system);
		
		exiting = (TextView) findViewById(R.id.exiting);
		exit_process = (ProgressBar) findViewById(R.id.exit_process);
		
		exit_system.setOnClickListener(this);
		back_system.setOnClickListener(this);
		
		Intent intent = getIntent();
		String msg = intent.getStringExtra("message");
		if(msg != null && msg.equals("EXIT")) {
			exit();
		}
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		Intent serviceIntent = new Intent(ExitActivity.this, QTService.class);
		stopService(serviceIntent);
	}

	public void onClick(View view) {
		switch(view.getId()){
		case R.id.exit_system:
			exit();
			break;
		case R.id.back_system:
			Intent intent = new Intent();
			intent.setClass(getApplicationContext(), MainControlActivity.class);
			startActivity(intent);
			finish();
			break;
		}
	}
	
	public void exit() {
		exiting.setVisibility(View.VISIBLE);
		exit_process.setVisibility(View.VISIBLE);
		tip.setVisibility(View.INVISIBLE);
		exittip.setVisibility(View.INVISIBLE);
		layout.setVisibility(View.GONE);
		ThreadPool.excuteLongTask(new Runnable() {
			@Override
			public void run() {
				int num = 0;
				while (num <= 100) {
					Message msg = new Message();  
					msg.arg1 = num;  
					handler.sendMessage(msg); 
					num++;
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		});
	}
}
